/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import domain.Mortgagecontract;
import domain.Userdom;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

/**
 *
 * @author Alejandro
 */
public class MortgageContractDao {
    EntityManager em;
    EntityTransaction tx;

    public MortgageContractDao() {
        em = SingletonDao.getInstance().getEntityManager();
        tx = SingletonDao.getTx();
    }

    public Mortgagecontract getMortgageContract(Integer idMortgageContract) {
        Mortgagecontract m = (Mortgagecontract) em.createNativeQuery("select * from mortgagecontract as n where n.idMortgageContract='" + idMortgageContract + "'", Mortgagecontract.class).getSingleResult();
        return m;
    }

    public void saveMortgageContract(Mortgagecontract m) {
        tx.begin();
        em.persist(m);
        tx.commit();
    }

    public void removeMortgageContract(Mortgagecontract m) {
        tx.begin();
        em.remove(m);
        tx.commit();
    }

    public List<Mortgagecontract> getMortgageContrList(Userdom user) {
        tx.begin();
        List list = em.createNativeQuery("select * from bankaccount b, contract c, mortgagecontract m where b.userFK='"+user.getNick()+"' and c.bankFK=b.idBank and m.contractFK=c.idContract", Mortgagecontract.class).getResultList();
        tx.commit();
        return list;
    }

    public void updateMortgageContract(Mortgagecontract m) {
        tx.begin();
        em.createNativeQuery("update mortgagecontract set dateOfExpiration='"+m.getDateOfExpiration()+"', company='"+m.getCompany()+"', total='"+m.getTotal()+"', interest='"+m.getInterest()+"' where idMortgageContract='"+m.getIdMortgageContract()+"'").executeUpdate();
        tx.commit();
    }
}
